import komand
# Custom imports below
from .schema import SearchInput, SearchOutput


class Search(komand.Action):
    def __init__(self):
        super(self.__class__, self).__init__(
                name='search',
                description='Search config',
                input=SearchInput(),
                output=SearchOutput())

    def run(self, params={}):
        search_term = params.get("search_word")
        search_type = params.get("type").lower()
        response = self.connection.client.search_config(search_type=search_type, term=search_term)
        self.logger.info("Got response {response}".format(response=response))
        status_code = response["response"]

        if not status_code == "200":
            raise Exception("Received non-200 status code")

        self.logger.info("Run: Received status code 200")

        results = list()
        items = response["items"]

        for key, value in items.items():
            results.append(value)

        return {"info": results}

    def test(self):
        """TODO: Test action"""
        return {}
